package com._21cn._front.cms.api;

import com._21cn.cms.common.utils.ResponseUtil;
import com._21cn.fetch.utils.*;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping({"/api/fetch"})
public class FetchApiController
{
    private String postUrl = "http://admin.cms.qiaoliba.com/admin/article/article/save.do";

    private static Integer WEB_SITE_60mil = 1;
    private static Integer WEB_SITE_Umei = 2;
    private static Integer WEB_SITE_Qiaoli = 3;

    @RequestMapping({"/fetchArticle"})
    public void fetchArticle(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        try {
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            String articleUrl = reqInfo.getParameter("url", "");
            Integer webSite = Integer.valueOf(reqInfo.getIntParameter("webSite", 0));
            if (!StringUtil.isEmpty(articleUrl)) {
                if (webSite.equals(WEB_SITE_60mil)) {
                    Fetch60mil.saveArticle(articleUrl);
                    ResponseUtil.returnJsonMsg(response, 0, "采集成功！（60mil）", null);
                }
                if (webSite.equals(WEB_SITE_Umei)) {
                    FetchUmei.saveArticle(articleUrl);
                    ResponseUtil.returnJsonMsg(response, 0, "采集成功！（优美）", null);
                }
                if (webSite.equals(WEB_SITE_Qiaoli)) {
                    FetchQiaoli.saveArticle(articleUrl);
                    ResponseUtil.returnJsonMsg(response, 0, "采集成功！（俏丽网）", null);
                }
            } else {
                ResponseUtil.returnJsonMsg(response, 2, "参数错误", null);
            }
        } catch (Exception e) {
            ResponseUtil.returnJsonMsg(response, 1, "服务器处理错误", null);
            e.printStackTrace();
        }
    }

}